package com.controller.wmt;

import com.entity.Otherregistration;
import com.entity.PreSaleService;
import com.entity.Service;
import com.service.wmt.OtherregistrationService;
import com.service.wmt.RegistrationtypeService;

import com.service.wmt.RoomService;

import com.service.wmt.ServiceService;
import com.util.gzp.AjaxResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

@Controller
@RequestMapping("presale")
public class PresalePermitRegistrationController {
    @Resource
    private RegistrationtypeService registrationtypeService;
    @Resource
    private ServiceService serviceService;

    @Resource
    private OtherregistrationService otherregistrationService;
    @Resource
    private RoomService roomService;


    /**
     * 查找所有的状态
     * @return
     */
    @RequestMapping("getAllServiceId")
    @ResponseBody
    public AjaxResponse getAllType(){
        AjaxResponse ajaxResponse = AjaxResponse.success( registrationtypeService.findAll());
        return ajaxResponse;

    }
    @RequestMapping("getAllServiceByName")
    @ResponseBody
    public AjaxResponse getAllServiceByName(String registrationtype){
        System.out.println("PresalePermitRegistrationController.getAllServiceByName"+registrationtype);
        AjaxResponse ajaxResponse = AjaxResponse.success( registrationtypeService.findByName(registrationtype));
        return ajaxResponse;

    }
    @RequestMapping("getAllService")
    @ResponseBody
    public AjaxResponse getAllService(){
        AjaxResponse ajaxResponse = AjaxResponse.success(serviceService.findServiceNotOk());
        return ajaxResponse;

    }

    /**
     * 查找所有的未受理的申请并显示
     * @return
     */
    @RequestMapping("getOaService")
    @ResponseBody
    public AjaxResponse getOaService(Integer pageNum,Integer pageSize){
        AjaxResponse ajaxResponse = AjaxResponse.success(serviceService.findAll(pageNum,pageSize));
        return ajaxResponse;

    }

    /**
     * 根据业务宗号查到相应的业务表数据
     * @param serviceid
     * @return
     */
    @RequestMapping("findByServiceId")
    @ResponseBody
    public AjaxResponse findByServiceId(Integer serviceid){
        AjaxResponse ajaxResponse = AjaxResponse.success(serviceService.findByServiceid(serviceid));
        return ajaxResponse;
    }


    /**
     * 添加预售许可
     * @param otherregistration
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public AjaxResponse addPreSale(Otherregistration otherregistration){
        System.out.println("PresalePermitRegistrationController.addPreSale"+otherregistration);
        //findService
        Service service =serviceService.findById(otherregistration.getServiceid());
        //changestate
        serviceService.changeStatu(service);
        Otherregistration otherregistration1 = new Otherregistration();
         otherregistration1.setPrevregisternumber(Number.toHex(new Date().getTime()));

        otherregistration1.setServiceid(service.getServiceid());
        otherregistration1.setRegistrationtypeid(service.getServicetypeid());
        otherregistration1.setObligee(service.getProposer());
        otherregistration1.setBoligeenumber(otherregistration.getBoligeenumber());
        otherregistration1.setObligor(service.getAgent());
        otherregistration1.setObligornumber(otherregistration.getObligornumber());
//        otherregistration1.setUid(1);
        otherregistration1.setPrevserviceid(19);
        otherregistration1.setRegistertime(new Date());
        otherregistration1.setRoomid(otherregistration.getRoomid());
        roomService.changeRoomStatue(otherregistration.getRoomid());
        AjaxResponse ajaxResponse = AjaxResponse.success(otherregistrationService.insert(otherregistration1));
        return ajaxResponse;

    }


}
